package cn.handong.tool.gui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.URL;
import java.util.*;

public class BfsGui {
    public static void main(String[] args) {
        // 初始化贪吃蛇游戏界面
        JFrame frame = new JFrame("路径规划"); // 创建一个窗体对象
        BfsPanel.m = 5;
        BfsPanel.n = 7; //列数
        frame.setSize(50+BfsPanel.n*25, 75+BfsPanel.m*25); // 设置窗体大小为900x800
        frame.setLocationRelativeTo(null); // 设置窗体为居中格式
        frame.setResizable(false); // 设置窗体不可改变
        frame.add(new BfsPanel());// 在窗体中添加一个面板
        frame.setVisible(true); // 设置窗体可见
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 结束窗口所在的应用程序
    }
}


// 存储图片
class BfsImageData {
    // 获取蛇身的图片
    public static URL bodyurl = BfsImageData.class.getResource("/material/body.png");
    public static ImageIcon body = new ImageIcon(bodyurl);
    // 获取食物的图片
    public static URL foodurl = ImageIcon.class.getResource("/material/food.png");
    public static ImageIcon food = new ImageIcon(foodurl);
    // 获取蛇头向下的图片
    public static URL redurl = BfsImageData.class.getResource("/material/red.png");
    public static ImageIcon red = new ImageIcon(redurl);
//    public static
}

// 用面板制作游戏界面
class BfsPanel extends JPanel implements KeyListener, ActionListener {
    String[][] maze = {{".",".",".",".",".",".","."},
                       {".","*",".","*",".",".","."},
                       {".","*",".","*",".",".","."},
                       {".","*","*","*",".",".","."},
                       {".",".",".",".","*",".","."}};
    String dir = "R";// 蛇头方向:R：向右，D:向下，L：向左，U：向上
    Queue<BfsNode> queue = new LinkedList<>();

    boolean flag = false;
    static int[] X = {0,0,1,-1};
    static int[] Y = {1,-1,0,0};
    static String[][] a;
    static int n;
    static int m;
    static int res = 0;
    static BfsNode S = new BfsNode();
    static BfsNode T = new BfsNode();

    public BfsPanel() {
        Init();
        this.setFocusable(true);// 获取焦点事件
        this.addKeyListener(this);// 添加键盘监听事件
//        timer.start(); // 时间开始
    }

    // 初始化游戏数据
    public void Init() {
        dir = "R";// 初始化蛇头方向向右
        flag = false;
        S.x = 2;
        S.y = 2;
        T.x = 4;
        T.y = 3;
        queue.offer(S);
//        a = maze;
        a = new String[m][n];
        n = maze.length;
        m = maze[0].length;
        for (int i = 0; i < a.length; i++) {
            System.arraycopy(maze[i], 0, a[i], 0, a[0].length);
        }
    }

    // 画面板
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g); // 清屏效果
        this.setBackground(Color.BLUE); // 设置面板的背景颜色
//        BfsImageData.header.paintIcon(this, g, 25, 10); // 绘制面板头部图片即（logo）
        g.fillRect(25, 25, 25*m, 25*n); // 绘制游戏区域
        // 绘制游戏积分板块
        g.setColor(Color.RED);
        //绘制迷宫
        for (int i = 0; i < maze.length; i++) {
            for (int j = 0; j < maze[0].length; j++) {
                if (Objects.equals(maze[i][j], "*")){
                    BfsImageData.food.paintIcon(this, g, 25+j*25, 25+i*25);
                }else if (Objects.equals(maze[i][j], "r")){
                    BfsImageData.body.paintIcon(this, g, 25+j*25, 25+i*25); //红色
                } else if (Objects.equals(maze[i][j] , "g")) {
                    BfsImageData.body.paintIcon(this, g, 25+j*25, 25+i*25); //绿色
//                    BfsImageData.body.paintIcon(this, g, 25+j*25, 25+i*25); //绿色
                }
            }
        }
        if (Objects.equals(dir, "T")){
            if (queue.isEmpty()){
                return;
            }
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                BfsNode node = queue.poll();
                if (node.x == T.x && node.y == T.y){
                    return;
                }
                for (int j = 0; j < 4; j++) {
                    int newX = node.x + X[j];
                    int newY = node.y + Y[j];
                    if (newX >= 0 && newX < n && newY >= 0 && newY < m && Objects.equals(a[newX][newY], ".")){
                        BfsNode newNode = new BfsNode();
                        newNode.x = newX;
                        newNode.y = newY;
                        queue.offer(newNode);
                        maze[newX][newY] = flag ? "r" : "g";
                        a[newX][newY] = "*";
                    }
                }
            }
            res++;
            flag = !flag;
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    // 键盘监听事件:按键控制游戏是否开始
    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_SPACE) {
            repaint();// 重新绘制界面
            dir = "T";
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void actionPerformed(ActionEvent e) {

    }
}
class BfsNode{
    int x;
    int y;
}